package com.xique.door.command.impl;

import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.AccessDevice;
import com.xique.door.bean.domain.CardDevice;
import com.xique.door.bean.domain.DCard;
import com.xique.door.bean.domain.User;
import com.xique.door.bean.dto.CardDeviceHouseCodeDTO;
import com.xique.door.bean.dto.UserDeviceHouseCodeDTO;
import com.xique.door.command.ICardCommandBizService;
import com.xique.door.constant.AccessCommandConstant;
import com.xique.door.device.command.AccessCommandHQService;
import com.xique.door.device.freeview.constant.CommandStatus;
import com.xique.door.device.freeview.service.biz.FreeviewCommandBizService;
import com.xique.door.device.hq.bean.dto.HQResultDTO;
import com.xique.door.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.map.SingletonMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: caogq
 * @Date: 2022/2/26 13:55
 * @Description:
 */
@Service
public class CardCommandBizServiceImpl implements ICardCommandBizService {

    @Value("${HQ_DEFAULT_PIC}")
    private String hqDefaultPic;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserDeviceService userDeviceService;

    @Autowired
    private AccessCommandHQService accessCommandHQService;

    @Autowired
    private IBuildHouseUserService houseUserService;
    @Autowired
    private FreeviewCommandBizService freeviewCommandBizService;

    @Autowired
    private IDCardDeviceService cardDeviceService;

    /**
     * @param card
     * @param dtos
     * @description: 下发卡片
     * @author caogq
     * @date: 2022/2/26 14:01
     * @Param
     */
    @Override
    public AjaxResult downCard(DCard card,List<CardDevice>  cardDevices, List<CardDeviceHouseCodeDTO> dtos) {
        if (cardDevices.isEmpty()){
            return AjaxResult.success();
        }
        User user = userService.selectUserById(card.getUserId());
        user.setFaceUrl(hqDefaultPic);
//        if (StringUtils.isEmpty(user.getFaceUrl())) {
//            //如果没有图片，设置默认图片
//        }
        //1、找出卡片用户所拥有的设备权限
        List<CardDevice> hqDevices = cardDevices.stream().filter(item -> AccessCommandConstant.DeviceType.HQ.equals(item.getModel())).collect(Collectors.toList());
        List<CardDevice> freeviewDevices = cardDevices.stream().filter(item -> AccessCommandConstant.DeviceType.FREEVIEW_45.equals(item.getModel())|| AccessCommandConstant.DeviceType.FREEVIEW_32.equals(item.getModel())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(dtos)){
            dtos = houseUserService.selectHouseCodesByCard(card);
        }

        if (!hqDevices.isEmpty()) {
            //海清卡片下发
            List<String> devSns = hqDevices.stream().map(CardDevice::getDevSn).collect(Collectors.toList());
            Object o = accessCommandHQService.downCardInfos(devSns, user, card, dtos);
        }

        if (!freeviewDevices.isEmpty()) {
            List<String> devSns = freeviewDevices.stream().map(CardDevice::getDevSn).collect(Collectors.toList());
            //全视通卡片下发
            freeviewCommandBizService.downCardInfo(devSns, card, dtos);
        }


        return AjaxResult.success();
    }

    @Override
    public AjaxResult downCards(List<AccessDevice> accessDevices, List<DCard> cards) {
        List<Long> cellDeviceIds = accessDevices.stream().filter(e -> e.getAccessType().equals("elevator")).map(AccessDevice::getId).collect(Collectors.toList());
        List<CardDeviceHouseCodeDTO> dtos = new ArrayList<>();
        if (!cellDeviceIds.isEmpty()){
            dtos = houseUserService.selectHouseCodesByCardDevice(cellDeviceIds, cards.stream().map(DCard::getId).collect(Collectors.toList()));
        }
        List<AccessDevice> hqAccessDevices = accessDevices.stream().filter(e -> e.getModel().equals(AccessCommandConstant.DeviceType.HQ)).collect(Collectors.toList());
        List<AccessDevice> freeviewAccessDevices = accessDevices.stream().filter(e -> Arrays.asList(AccessCommandConstant.DeviceType.FREEVIEW_32, AccessCommandConstant.DeviceType.FREEVIEW_45).contains(e.getModel())).collect(Collectors.toList());

        if (!hqAccessDevices.isEmpty()) {
            Object o = accessCommandHQService.downBatchCardInfos(hqAccessDevices, cards, dtos);
        }
        if (!freeviewAccessDevices.isEmpty()) {
            freeviewCommandBizService.downCardInfos(freeviewAccessDevices, cards, dtos);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteCard(DCard card,List<CardDevice> cardDevices) {

        for (CardDevice device : cardDevices) {
            device.setStatus(CommandStatus.SEND);
            device.setUpdateTime(new Date());
            device.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
            cardDeviceService.updateDCardDevice(device);
        }
        List<CardDevice> hqCardDevice = cardDevices.stream().filter(item -> AccessCommandConstant.DeviceType.HQ.equals(item.getModel())).collect(Collectors.toList());
        List<CardDevice> freeviewCardDevice = cardDevices.stream().filter(item -> AccessCommandConstant.DeviceType.FREEVIEW_45.equals(item.getModel())|| AccessCommandConstant.DeviceType.FREEVIEW_32.equals(item.getModel())).collect(Collectors.toList());
        if (!hqCardDevice.isEmpty()) {
            //海清卡片删除
            List<String> devSns = cardDevices.stream().map(CardDevice::getDevSn).collect(Collectors.toList());
            Object o = accessCommandHQService.delUserInfos(devSns, new SingletonMap<>("customId", card.getCustomId()), AccessCommandConstant.VERSION);
        }

        if (!freeviewCardDevice.isEmpty()) {
            //全视通卡片下发
            List<String> devSns = freeviewCardDevice.stream().map(CardDevice::getDevSn).collect(Collectors.toList());
            freeviewCommandBizService.deleteCardInfo(devSns, card);
        }


        return AjaxResult.success();
    }

    /**
     * @param
     * @description: 挂失卡片
     * @author caogq
     * @date: 2022/2/26 16:32
     * @Param
     */
    @Override
    public AjaxResult lossCard(DCard card) {
        User user = userService.selectUserById(card.getUserId());
        user.setCustomId(card.getCustomId());
        card.setOriginCardSn(card.getCardSn());
        CardDevice cardDevice = new CardDevice();
        cardDevice.setCardSn(card.getCardSn());
        List<CardDevice> cardDevices = cardDeviceService.selectDCardDeviceList(cardDevice);
        List<CardDeviceHouseCodeDTO> dtos = houseUserService.selectHouseCodesByCard(card);

        for (CardDevice device : cardDevices) {
            device.setStatus(CommandStatus.SEND);
            device.setUpdateTime(new Date());
            device.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
            cardDeviceService.updateDCardDevice(device);
        }
        card.setCardSn("0000000000");
        List<CardDevice> hqCardDevice = cardDevices.stream().filter(item -> AccessCommandConstant.DeviceType.HQ.equals(item.getModel())).collect(Collectors.toList());
        List<CardDevice> freeviewCardDevice = cardDevices.stream().filter(item -> AccessCommandConstant.DeviceType.FREEVIEW_45.equals(item.getModel())|| AccessCommandConstant.DeviceType.FREEVIEW_32.equals(item.getModel())).collect(Collectors.toList());

        if (!hqCardDevice.isEmpty()) {
            //海清卡片下发
            List<String> devSns = cardDevices.stream().map(CardDevice::getDevSn).collect(Collectors.toList());
//            Object o = accessCommandHQService.downCardInfos(devSns, user, card,dtos);
        }

        if (!freeviewCardDevice.isEmpty()) {
            List<String> devSns = freeviewCardDevice.stream().map(CardDevice::getDevSn).collect(Collectors.toList());
            //全视通卡片下发
//            freeviewCommandBizService.downCardInfo(devSns, card,dtos);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult unLossCard(DCard card) {

        User user = userService.selectUserById(card.getUserId());
        user.setCustomId(card.getCustomId());
        user.setCardSn(card.getCardSn());
        CardDevice cardDevice = new CardDevice();
        cardDevice.setCardSn(card.getCardSn());
        List<CardDevice> cardDevices = cardDeviceService.selectDCardDeviceList(cardDevice);
        List<CardDeviceHouseCodeDTO> dtos = houseUserService.selectHouseCodesByCard(card);
        for (CardDevice device : cardDevices) {
            device.setStatus(CommandStatus.SEND);
            device.setUpdateTime(new Date());
            device.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
            cardDeviceService.updateDCardDevice(device);
        }
        List<CardDevice> hqCardDevice = cardDevices.stream().filter(item -> AccessCommandConstant.DeviceType.HQ.equals(item.getModel())).collect(Collectors.toList());
        List<CardDevice> freeviewCardDevice = cardDevices.stream().filter(item -> AccessCommandConstant.DeviceType.FREEVIEW_45.equals(item.getModel())|| AccessCommandConstant.DeviceType.FREEVIEW_32.equals(item.getModel())).collect(Collectors.toList());

        if (!hqCardDevice.isEmpty()) {
            //海清卡片下发
            List<String> devSns = cardDevices.stream().map(CardDevice::getDevSn).collect(Collectors.toList());
//            Object o = accessCommandHQService.downCardInfos(devSns, user, card,dtos );
        }

        if (!freeviewCardDevice.isEmpty()) {
            List<String> devSns = freeviewCardDevice.stream().map(CardDevice::getDevSn).collect(Collectors.toList());
            //全视通卡片下发
//            freeviewCommandBizService.downCardInfo(devSns, card,dtos);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult downCards(Long deviceId) {
        CardDevice cardDevice = new CardDevice();
        cardDevice.setDeviceId(deviceId);
        List<CardDevice> cardDevices = cardDeviceService.selectDCardDeviceList(cardDevice);
        List<CardDevice> hqCardDevice = cardDevices.stream().filter(item -> AccessCommandConstant.DeviceType.HQ.equals(item.getModel())).collect(Collectors.toList());
        List<CardDevice> freeviewCardDevice = cardDevices.stream().filter(item -> AccessCommandConstant.DeviceType.FREEVIEW_45.equals(item.getModel())|| AccessCommandConstant.DeviceType.FREEVIEW_32.equals(item.getModel())).collect(Collectors.toList());
        if (!hqCardDevice.isEmpty()) {
            //海清卡片下发
            List<String> devSns = cardDevices.stream().map(CardDevice::getDevSn).collect(Collectors.toList());
//            Object o = accessCommandHQService.downCardInfos(devSns, user, card, AccessCommandConstant.VERSION);
        }

        if (!freeviewCardDevice.isEmpty()) {
            List<String> devSns = freeviewCardDevice.stream().map(CardDevice::getDevSn).collect(Collectors.toList());
            //全视通卡片下发
//            freeviewCommandBizService.downCardInfos(devSns, card);
        }
        return AjaxResult.success();
    }
}
